/*
 * Copyright (C) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <unistd.h> 
#include "cmsis_os2.h"
#include "lwip/sockets.h"
#include "profile_package.h"
#include "aht20.h"
#include "HiSpark_WiFi_IoT_EM_VER.h"

typedef struct
{
	int temp;                         //温度
    int humi;                         //湿度
    int ppm;                          //烟雾浓度
} report_t;

extern int new_fd,sock_fd,beep_status;;
int is_accepted;
/**************************************************
 * deal_report_msg
 * 处理数据，并使用TCP协议上报数据
 * ***********************************************/
static void deal_report_msg(report_t *report)
{
    char *msg;
    uint8_t ret;
    profile_service_t    service;
    profile_kv_t         temp;
    profile_kv_t         humi;
    profile_kv_t         ppm;
    profile_kv_t         beep;

    //配置要发送的数据
    service.event_time = NULL;
    service.service_id = "smart_environment";
    service.service_property = &temp;
    service.nxt = NULL;

    temp.key = "temp";
    temp.value = &report->temp;
    temp.type = PROFILE_VALUE_INT;
    temp.nxt = &humi;

    humi.key = "humi";
    humi.value = &report->humi;
    humi.type = PROFILE_VALUE_INT;
    humi.nxt = &ppm;

    ppm.key = "ppm";
    ppm.value = &report->ppm;
    ppm.type = PROFILE_VALUE_INT;
    ppm.nxt = &beep;

    beep.key = "Beep";
    beep.value = beep_status?"ON":"OFF";
    beep.type = PROFILE_VALUE_STRING;
    beep.nxt = NULL;

    //打包数据
    msg = profile_package_propertyreport(&service);
    
    //发送数据
    if(send(new_fd, msg, strlen(msg), 0) < 0)
    {
        printf("send error\n");
        close(new_fd);
    }
    return;
}

/**************************************************
 * 任务：report_message_task
 * 上报传感器任务
 * ***********************************************/
int report_message_task(void)
{
    printf("report_message_task\r\n");
    report_t *report_msg;
    Environment_Data_TypeDef data;

    //客户端地址信息
	struct sockaddr_in client_sock;
	int sin_size;

    /* 初始化传感器 */
    EnvironmentInit(); 
    report_msg = malloc(sizeof(report_t));
    while (1)
    {
        EnvironmentTask(&data);
        if(is_accepted == 1)
        {
            if (NULL != report_msg)
            {
                /* 打包数据并将数据发送到队列中 */
                report_msg->temp = (int)data.temp;
                report_msg->humi = (int)data.humi;
                report_msg->ppm = (int)data.ppm;
                deal_report_msg(report_msg);
            }
        }
        osDelay(100);
    }
    free(report_msg);
    return 0;
}